package com.unifiedlibrary.amall_page.base.recycler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xzk
 * @data 2019/5/7
 * @email xiezhengkun@beyondsoft.com
 * @remark  recyclerView的基类适配器
 */
public abstract class RcvArrBaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    public Context context;
    private int checkPoi=-1;
    private int layoutId;
    private T[] dataList ;
    protected ItemOnClickListener itemOnClickListener;

    public int getCheckPoi() {
        return checkPoi;
    }

    public void setCheckPoi(int checkPoi) {
        this.checkPoi = checkPoi;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public interface ItemOnClickListener<T> {
        void onItem(T t, int position);
    }

    public RcvArrBaseAdapter(Context context, int layoutId) {
        this.context = context;
        this.layoutId = layoutId;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(context, itemView);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        if(dataList== null){
            return;
        }
        bindViewHolder(holder, dataList[position], position);
        holder.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onItem(dataList[position], position);
                }

            }
        } );
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.length;
    }

    public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position);

    public Context getContext() {
        return context;
    }

    public T[] getDataList() {
        return dataList;
    }

    public void setDataList(T[] dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
}